package com.woniuxy.list.controller;

import com.hc.entity.RankList;
import com.hc.utils.ResponseEntity;
import com.woniuxy.list.service.ListService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/list")
@CrossOrigin
public class ListController {
    @Resource
    private ListService listService;
    //添加一个榜单
    @PostMapping("/insertList")
    public ResponseEntity<Boolean> addARankList(@RequestBody RankList rankList){
        ResponseEntity<Boolean> entity;
        int flag = listService.addList(rankList);
        if(flag>0){
            entity=ResponseEntity.SUCCESS;
            entity.setData(true);
            return entity;
        }
        entity=ResponseEntity.FAIL;
        entity.setData(false);
        return entity;
    }
    //根据id修改榜单分数
    @PutMapping("/updateScoreById/{id}/{score}")
    public ResponseEntity<Boolean> updateScoreById(@PathVariable("id")Integer id,@PathVariable("score")Integer score){
        ResponseEntity<Boolean> entity;
        int flag = listService.updateScoreByListId(id, score);
        if(flag>0){
            entity=ResponseEntity.SUCCESS;
            entity.setData(true);
            return entity;
        }
        entity=ResponseEntity.FAIL;
        entity.setData(false);
        return entity;
    }
    //获取榜单所有数据
    @GetMapping("/getAllRankList")
    public ResponseEntity<List<RankList>> getAll(){
        ResponseEntity<List<RankList>> entity;
        List<RankList> allList = listService.getAllList();
        if(allList.size()>0){
            entity=ResponseEntity.SUCCESS;
            entity.setData(allList);
            return entity;
        }
        entity=ResponseEntity.FAIL;
        entity.setData(null);
        return entity;
    }
    //获取不同种类榜单的所有数据
    @GetMapping("/getByClassId/{id}")
    public ResponseEntity<List<RankList>> getByClassId(@PathVariable("id")Integer id){
        ResponseEntity<List<RankList>> entity;
        List<RankList> listByClassId = listService.getListByClassId(id);
        if(listByClassId.size()>0){
            entity=ResponseEntity.SUCCESS;
            entity.setData(listByClassId);
            return entity;
        }
        entity=ResponseEntity.FAIL;
        entity.setData(null);
        return entity;
    }
}
